#include"groupmodel.hpp"
#include"json.hpp"
#include"CommonConnectionPool.hpp"

using json = nlohmann::json;

//创建群组
bool GroupModel::createGroup(Group &group){
    //组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "insert into AllGroup(groupname, groupdesc) values('%s', '%s')", 
            group.getName().c_str(),
            group.getDesc().c_str()
            );

    //从连接池中获取一个mysql连接
    ConnectionPool* sqlConnPool = ConnectionPool::getConnectionPool();
    auto sqlConn = sqlConnPool->getConnection();

    //更新数据库
    bool state = sqlConn->update(sql);
    if(state){
        group.setId(sqlConn->getInsertId());
        return true;
    }
    return false;
}

//加入群组
bool GroupModel::addGroup(int userid, int groupid, string role){
    //检测是否存在groupid
    //组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "select * from AllGroup where id = %d", 
            groupid
            );

    //从连接池中获取一个mysql连接
    ConnectionPool* sqlConnPool = ConnectionPool::getConnectionPool();
    auto sqlConn = sqlConnPool->getConnection();

    //更新数据库
    MYSQL_RES* res = sqlConn->query(sql);
    //groupid不存在
    if(res){
        MYSQL_ROW row = mysql_fetch_row(res);
        if(row==nullptr)return false;
    }
    mysql_free_result(res);//一定要释放结果集，负责mysql无法再次查询

    //组装sql语句
    char sql1[1024] = {0};
    sprintf(sql1, "insert into GroupUser values(%d, %d, '%s')", 
            groupid, userid, role.c_str()
            );

    //更新数据库
    return sqlConn->update(sql1);
}

//查询群组用户所在组的信息
vector<Group> GroupModel::queryGroups(int userid){
    //组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "select a.id, a.groupname, a.groupdesc from AllGroup a inner join GroupUser b on a.id = b.groupid where b.userid = %d", 
            userid
            );

    //从连接池中获取一个mysql连接
    ConnectionPool* sqlConnPool = ConnectionPool::getConnectionPool();
    auto sqlConn = sqlConnPool->getConnection();

    //执行查询操作
    vector<Group> vec;
    MYSQL_RES* res = sqlConn->query(sql);
    if(res){
        MYSQL_ROW row;
        while((row = mysql_fetch_row(res)) != nullptr){
            vec.emplace_back(Group(stoi(row[0]), row[1], row[2]));
        }
    }
    mysql_free_result(res);

    //查询每一个group中的user信息
    for(Group &group : vec){
        sprintf(sql, "select a.id, a.name, a.state, b.grouprole from User a inner join GroupUser b on a.id = b.userid where b.groupid = %d", 
                group.getId()
        );
        
        MYSQL_RES* res = sqlConn->query(sql);
        if(res){
            MYSQL_ROW row;
            while((row = mysql_fetch_row(res)) != nullptr){
                group.getUsers().emplace_back(GroupUser(row[3],stoi(row[0]), row[1], "", row[2]));
                
            }
        }
        mysql_free_result(res);
    }
    return vec;
}

//根据指定的groupid查询群组用户id列表，除了userid自己，用于群发消息
vector<int> GroupModel::queryGroupUsers(int userid, int groupid){
    //组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "select userid from GroupUser where groupid = %d and userid != %d", 
            groupid,
            userid
            );

    //从连接池中获取一个mysql连接
    ConnectionPool* sqlConnPool = ConnectionPool::getConnectionPool();
    auto sqlConn = sqlConnPool->getConnection();

     //执行查询操作
    vector<int> vec;
    MYSQL_RES* res = sqlConn->query(sql);
    if(res){
        MYSQL_ROW row;
        while((row = mysql_fetch_row(res)) != nullptr){
            vec.emplace_back(atoi(row[0]));
        }
    }
    mysql_free_result(res);
    return vec;
}
